
import inkex


## ------------------------------------------------------------
## Copyright 2010 Pierre-Antoine Delsart
## 
## This file is part of LeVin.
## 
## LeVin is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## 
## LeVin is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with LeVin.  If not, see <http://www.gnu.org/licenses/>.
## ---------------------------------------------------------------
#import cubicsuperpath, simplestyle, copy, math, re, bezmisc
from simplestyle import *
import copy, simpletransform, simplepath

class SplitIt(inkex.Effect):
    N = 0
    cout = None
    def __init__(self):
        inkex.Effect.__init__(self)

    def effect(self):
        #print 'effect', e.selected
        svg = self.document.getroot()
        #layer = inkex.etree.SubElement(svg, 'g')
        fG = self.getElementById('fullGlass')
        hG = self.getElementById('halfGlass')
        p = fG.getparent()        

        nfull , nhalf = divmod(self.N,2)
        bbox = simpletransform.computeBBox( [fG] )
        fullwidth = int(bbox[1]-bbox[0])

        #print '!!!!!',fullwidth
        allGlasses = [ self.copyFullGlass( fG , i , fullwidth ) for i in range(1,nfull) ]
        if nhalf==1:
            self.translatePath(hG, fullwidth*nfull)
        else:
            p.remove(hG)
        if nfull == 0:
            p.remove(fG)

        for g in allGlasses:
            p.append(g)
        self.allGlasses = allGlasses
        pass

    def translatePath(self,p,dx):
        path = simplepath.parsePath( p.attrib['d'] )
        simplepath.translatePath( path , dx,0)
        p.set('d', simplepath.formatPath( path ) )
        
    def copyFullGlass(self,fg, pos, width):
        cp = copy.deepcopy( fg)
        self.translatePath(cp, width*pos)
        return cp

import sys
def redirectStdout(name):
    f = open(name,'w')
    sys.stdout = f
    return f
if __name__ == '__main__':
    SplitIt.cout = sys.stdout
    e = SplitIt()
    for i in range(11):        
        e.N = i
        f=redirectStdout('ratingGlass'+str(i)+'.svg')
        e.affect()
        f.close()
    fg = e.getElementById('fullGlass')
    hg = e.getElementById('halfGlass')    
        
